package com.haier.nengyuan.myapplication;


import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class MonthFragment extends BaseFragment {

    private FrameLayout frameLayout;
    private TextView mTvMonEarningTotal;
    private String mStrMonth;
    private ArrayList<PointF> mPointArrayList = new ArrayList<>();
    private float mMonEarningTotal;
    private float monTal;
    private float currDayEnergy;
    private float currHourEnergy;
    private String mStrDay;
    private String mStrHour;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank, container, false);
        frameLayout = (FrameLayout) view.findViewById(R.id.frame_layout);
        mTvMonEarningTotal = (TextView) view.findViewById(R.id.tv_mon_earning_total);
        return view;
    }
//
//    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
//    @Override
//    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        Log.e("---", "onViewCreated: " + mStrMonth);
//        mStrMonth = MyDateUtil.changeM(System.currentTimeMillis());
//        mStrDay = MyDateUtil.changeD(System.currentTimeMillis());
//        mStrHour = MyDateUtil.changeH(System.currentTimeMillis());
//        int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
//        if (mStrMonth.equals("09")) {
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
//                if (i == Integer.valueOf(mStrMonth) - 1) {
//                    /*当前月*/
//                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
//                        if (k == Integer.valueOf(mStrDay) - 1) {
//                            /*当前天*/
//                            for (int j = 0; j <= mIntHour1; j++) {
//                                /*时间段计算*/
////                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
//                                currHourEnergy += (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97));
//
//                            }
//                        } else {
//                            /*非当前天*/
//                            currDayEnergy += Float.valueOf(Variable.nineDayEnergy[k]);
//
//                        }
////                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);
//
//                    }
//                    mPointArrayList.add(new PointF(points4[i][0], (float) (0.85 * (currDayEnergy + currHourEnergy))));
//                } else {
//                    /*非当前月*/
//                    monTal += Variable.nineMonthEnergy[i];
//                    mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineMonthEnergy[i]) * 0.85)));
//
//                }
//                Log.e("---", "onViewCreated: mPointArrayList.get(i).y:" + mPointArrayList.get(i).y);
//                float y = mPointArrayList.get(i).y;
//                mMonEarningTotal = mMonEarningTotal + y;
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 1.2 - 50));//月累计发电量柱状图宽高
//                layoutParams.gravity = Gravity.BOTTOM;
////                layoutParams.setMarginStart((width) * i + 60 + 25);
////                layoutParams.setMarginEnd((width) * i + 60 + 25);
//                if (i == 0) {
//                    layoutParams.setMarginStart((width) * i + 60 + 10);
//
//                } else {
//                    layoutParams.setMarginStart((width) * i + 60 + 10 + 9 * i);
//
//                }
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//        }
//        if (mStrMonth.equals("10")) {
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
//                if (i == Integer.valueOf(mStrMonth) - 1) {
//                    /*当前月*/
//                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
//                        if (k == Integer.valueOf(mStrDay) - 1) {
//                            /*当前天*/
//                            for (int j = 0; j <= mIntHour1; j++) {
//                                /*时间段计算*/
////                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.tenDayEnergy[i]) * (points4[i][1] / 31.97))));
//                                currHourEnergy += (float) (Float.valueOf(Variable.tenDayEnergy[i]) * (points4[i][1] / 31.97));
//
//                            }
//                        } else {
//                            /*非当前天*/
//                            currDayEnergy += Float.valueOf(Variable.tenDayEnergy[k]);
//
//                        }
////                        energyTotal += Float.valueOf(Variable.tenDayEnergy[k]);
//
//                    }
//                    mPointArrayList.add(new PointF(points4[i][0], (float) (0.85 * (currDayEnergy + currHourEnergy))));
//                } else {
//                    /*非当前月*/
//                    monTal += Variable.nineMonthEnergy[i];
//                    mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineMonthEnergy[i]) * 0.85)));
//
//                }
//                Log.e("---", "onViewCreated: mPointArrayList.get(i).y:" + mPointArrayList.get(i).y);
//                float y = mPointArrayList.get(i).y;
//                mMonEarningTotal = mMonEarningTotal + y;
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 1.2 - 50));//月累计发电量柱状图宽高
//                layoutParams.gravity = Gravity.BOTTOM;
////                layoutParams.setMarginStart((width) * i + 60 + 25);
////                layoutParams.setMarginEnd((width) * i + 60 + 25);
//                if (i == 0) {
//                    layoutParams.setMarginStart((width) * i + 60 + 10);
//
//                } else {
//                    layoutParams.setMarginStart((width) * i + 60 + 10 + 9 * i);
//
//                }
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//        }
//        if (mStrMonth.equals("11")) {
//            WindowManager wm = getActivity().getWindowManager();
//            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
//            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
//                if (i == Integer.valueOf(mStrMonth) - 1) {
//                    /*当前月*/
//                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
//                        if (k == Integer.valueOf(mStrDay) - 1) {
//                            /*当前天*/
//                            for (int j = 0; j <= mIntHour1; j++) {
//                                /*时间段计算*/
////                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.eleEnergy[i]) * (points4[i][1] / 31.97))));
//                                currHourEnergy += (float) (Float.valueOf(Variable.eleEnergy[i]) * (points4[i][1] / 31.97));
//
//                            }
//                        } else {
//                            /*非当前天*/
//                            currDayEnergy += Float.valueOf(Variable.eleEnergy[k]);
//
//                        }
////                        energyTotal += Float.valueOf(Variable.eleEnergy[k]);
//
//                    }
//                    mPointArrayList.add(new PointF(points4[i][0], (float) (0.85 * (currDayEnergy + currHourEnergy))));
//                } else {
//                    /*非当前月*/
//                    monTal += Variable.nineMonthEnergy[i];
//                    mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineMonthEnergy[i]) * 0.85)));
//
//                }
//                Log.e("---", "onViewCreated: mPointArrayList.get(i).y:" + mPointArrayList.get(i).y);
//                float y = mPointArrayList.get(i).y;
//                mMonEarningTotal = mMonEarningTotal + y;
//                View view1 = new View(getActivity());
//                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 1.2 - 50));//月累计发电量柱状图宽高
//                layoutParams.gravity = Gravity.BOTTOM;
////                layoutParams.setMarginStart((width) * i + 60 + 25);
////                layoutParams.setMarginEnd((width) * i + 60 + 25);
//                if (i == 0) {
//                    layoutParams.setMarginStart((width) * i + 60 + 10);
//
//                } else {
//                    layoutParams.setMarginStart((width) * i + 60 + 10 + 9 * i);
//
//                }
//                view1.setLayoutParams(layoutParams);
//                view1.setBackgroundColor(0xffffffff);
//
//                frameLayout.addView(view1);
//            }
//        }
//
//        /*总收益*/
//        mTvMonEarningTotal.setText((float) (Math.round(0.85 * (monTal + currDayEnergy + currHourEnergy) * 100)) / 100 + "");
//
//
//    }

    private float mDataTotal;
    private float[] dayEnergy;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onResume() {
        super.onResume();
        mDataTotal = 0;
        frameLayout.removeAllViews();

        WindowManager wm = getActivity().getWindowManager();
//        int width = (wm.getDefaultDisplay().getWidth() - 160) / 12;
        int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
        long time = System.currentTimeMillis();
        int month = Integer.parseInt(MyDateUtil.changeM(time));
        int mIntD = Integer.parseInt(MyDateUtil.changeD(time));
        int hour = Integer.valueOf(MyDateUtil.changeH(time)) / 2;
        if (month == 9) {
            dayEnergy = VirtualData.nineDayEnergy;
        } else if (month == 10) {
            dayEnergy = VirtualData.tenDayEnergy;
        } else if (month == 11) {
            dayEnergy = VirtualData.eleEnergy;
        } else if (month == 12) {
            dayEnergy = VirtualData.decemberEnergy;
        } else if (month == 1) {
            dayEnergy = VirtualData.oneEnergy;
        }
        float mD = 0;
        for (int i = 0; i < mIntD - 1; i++) {
            mD += dayEnergy[i];
        }
        for (int i = 0; i <= hour; i++) {
            mD += (dayEnergy[mIntD - 1] * (VirtualData.pointsDay[i][1] / VirtualData.mdataDay));
        }
        for (int i = 0; i < month; i++) {
            float y;
            if (i + 1 == month) {
                y = mD;
            } else {
                y = VirtualData.pointsMouth[i];
            }
            mDataTotal += y;

            View view1 = new View(getActivity());
//            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 20, (int) (y * 0.88 * 0.7));
//            layoutParams.gravity = Gravity.BOTTOM;
//            layoutParams.setMarginStart((width) * i + 60 + 10);

            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width - 15, (int) (y * 1.2 - 50));//月累计发电量柱状图宽高
            layoutParams.gravity = Gravity.BOTTOM;
//                layoutParams.setMarginStart((width) * i + 60 + 25);
//                layoutParams.setMarginEnd((width) * i + 60 + 25);
            if (i == 0) {
                layoutParams.setMarginStart((width) * i + 60 + 10);

            } else {
                layoutParams.setMarginStart((width) * i + 60 + 10 + 9 * i);

            }

            view1.setLayoutParams(layoutParams);
            view1.setBackgroundColor(0xffffffff);

            frameLayout.addView(view1);
        }
        mTvMonEarningTotal.setText((Math.round(mDataTotal * 0.85 * 100)) / 100f + "");
    }

    @Override
    public int setRootView() {
        return R.layout.fragment_blank;
    }

    @Override
    public void initViews() {

    }

}
